<?php
namespace app\overlord\model;

use app\BaseModel;
use app\restaurant\model\Store;
use think\facade\Db;

class Active extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_overlord_atv';

    protected $append = [

        'cate_name',

        'signup_status',

        'atv_status',

        'signup_time_text',

        'atv_time_text',
    ];


    /**
     * @author chenniang
     * @DataTime: 2020-11-26 16:26
     * @功能说明:转换时间格式
     */
    public function getSignupTimeTextAttr($value,$data){

        if(isset($data['signup_start_time'])&&isset($data['signup_end_time'])){

            return date('m/d H:i',$data['signup_start_time']).' ~ '.date('m/d H:i',$data['signup_end_time']);
        }

    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-26 16:26
     * @功能说明:转换时间格式
     */
    public function getAtvTimeTextAttr($value,$data){

//        dump($data);exit;

        if(isset($data['atv_start_time'])&&isset($data['atv_end_time'])){

            return date('m/d H:i',$data['atv_start_time']).' ~ '.date('m/d H:i',$data['atv_end_time']);
        }

    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-26 16:07
     * @功能说明:活动状态
     */
    public function getSignupStatusAttr($value,$data){

        if(isset($data['signup_start_time'])&&isset($data['signup_end_time'])){
            //进行中
            $status = 2;
            //未开始
            if($data['signup_start_time']>time()){

                $status = 1;
            }
            //已结束
            if($data['signup_end_time']<time()){

                $status = 3;
            }

            return $status;

        }


    }

    /**
     * @author chenniang
     * @DataTime: 2020-11-26 16:07
     * @功能说明:活动状态
     */
    public function getAtvStatusAttr($value,$data){

        if(isset($data['atv_start_time'])&&isset($data['atv_end_time'])){
            //进行中
            $status = 2;
            //未开始
            if($data['atv_start_time']>time()){

                $status = 1;
            }

            //dump($data['atv_end_time']);exit;
            //已结束
            if($data['atv_end_time']<time()){

                $status = 3;
            }

            return $status;

        }


    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-25 16:39
     * @功能说明:
     */
    public function getImgsAttr($value,$data){

        if(!empty($value)){

            return explode(',',$value);
        }

    }

    /**
     * @author chenniang
     * @DataTime: 2020-11-25 16:39
     * @功能说明:
     */
    public function getInfoImgsAttr($value,$data){

        if(!empty($value)){

            return explode(',',$value);
        }

    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-25 10:56
     * @功能说明:分类名字
     */
    public function getCateNameAttr($value,$data){

        if(!empty($data['cate_id'])){

            $cate_model = new Cate();

            $name = $cate_model->where(['id'=>$data['cate_id']])->value('title');

            return $name;
        }

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:内容添加
     */
    public function dataAdd($data){

        $data['create_time'] = time();

        $data['update_time'] = time();

        $data['imgs'] = !empty($data['imgs'])?implode(',',$data['imgs']):'';

        $data['info_imgs']  = !empty($data['info_imgs'])?implode(',',$data['info_imgs']):'';

        $data['total_cash'] = $data['cash']*$data['num'];

        $content = $data['content'];

        unset($data['content']);

        $store = $data['store'];

        unset($data['store']);

        $res = $this->insert($data);

        $id  = $this->getLastInsID();

        $this->updateSome($id,$data['uniacid'],$content,$store);

        return $id;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:内容编辑
     */
    public function adminDataUpdate($dis,$data){

        $data['imgs'] = !empty($data['imgs'])?implode(',',$data['imgs']):'';

        $data['info_imgs'] = !empty($data['info_imgs'])?implode(',',$data['info_imgs']):'';

        $data['total_cash'] = $data['cash']*$data['num'];

        $data['update_time'] = time();

        $content = $data['content'];

        unset($data['content']);

        $store = $data['store'];

        unset($data['store']);

        $res = $this->where($dis)->update($data);

        $this->updateSome($dis['id'],$dis['uniacid'],$content,$store);

        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-11-24 17:20
     * @功能说明:添加相关设置
     */
    public function updateSome($atv_id,$uniacid,$content,$store){

        Db::name('longbing_card_overlord_content_atv')->where(['atv_id'=>$atv_id])->delete();

        Db::name('longbing_card_overlord_store_atv')->where(['atv_id'=>$atv_id])->delete();

        if(!empty($content)){

            foreach ($content as $value){

                $insert = [

                    'uniacid' => $uniacid,

                    'atv_id'  => $atv_id,

                    'content_id' => $value
                ];

                Db::name('longbing_card_overlord_content_atv')->insert($insert);
            }
        }

        if(!empty($store)){

            foreach ($store as $value){

                $insert = [

                    'uniacid' => $uniacid,

                    'atv_id'  => $atv_id,

                    'store_id' => $value
                ];

                Db::name('longbing_card_overlord_store_atv')->insert($insert);
            }
        }

        return true;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-02 17:21
     * @功能说明:检测
     */
    public function check($data){

        if($data['signup_start_time']>=$data['atv_start_time']){

            return ['code'=>500,'msg'=>'报名开始时间必须小于活动开始时间'];
        }

        if($data['signup_end_time']>=$data['atv_end_time']){

            return ['code'=>500,'msg'=>'报名结束时间必须小于活动结束时间'];
        }

        return true;
    }


    public function checkNum($data){

        $id = $data['id'];

        $num = $data['num'];

        $record_mdoel = new Record();

        $res = $record_mdoel->where(['atv_id'=>$id])->field('atv_id,id,store_id,count(*) as sum')
            ->group('store_id')
            ->select()
            ->toArray();

        $a_num = 0;

        if(!empty($res)){

            foreach ($res as &$v){

                if($v['sum']>$a_num){

                    $a_num = $v['sum'];
                }

            }

        }

       if($num<$a_num){

           return ['code'=>500,'msg'=>'最低报名数'.$a_num];

       }


        return $res;

    }

    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page){

        $data = $this->where($dis)->order('id desc')->paginate($page)->toArray();

        return $data;

    }

    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function indexDataList($dis,$page){

        $data = $this->alias('a')
                ->join('longbing_card_overlord_store_atv b','a.id = b.atv_id')
                ->where($dis)
                ->field('a.*')
                ->group('a.id')
                ->order('a.id desc')
                ->paginate($page)
                ->toArray();


        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-25 16:56
     * @功能说明:被选中的门店
     */
    public function selectStore($atv_id){

//        $store_model = new Store();

        $dis = [

            'b.atv_id' => $atv_id
        ];

        $data = Db::name('longbing_card_restaurant_store')->alias('a')
                ->join('longbing_card_overlord_store_atv b','a.id = b.store_id')
                ->where($dis)
                ->field('a.id,a.store_type')
                ->group('a.id')
                ->select()
                ->toArray();

        return $data;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-25 16:56
     * @功能说明:被选中的门店
     */
    public function selectStoreIndex($atv_id){

        $dis = [

            'b.atv_id' => $atv_id
        ];

        $data = Db::name('longbing_card_restaurant_store')->alias('a')
            ->join('longbing_card_overlord_store_atv b','a.id = b.store_id')
            ->where($dis)
            ->field('a.title,a.province,a.city,a.area,a.address,a.long,a.latitude,a.top')
            ->group('a.id')
            ->order('a.top')
            ->select()
            ->toArray();

        return $data;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-27 14:54
     * @功能说明:获取表头
     */
    public function getTitle($atv_id){

        $dis[] = ['b.atv_id','=',$atv_id];

        $dis[] = ['a.status','>',-1];

        $data = Db::name('longbing_card_overlord_content')->alias('a')
                ->join('longbing_card_overlord_content_atv b','b.content_id = a.id')
                ->where($dis)
                ->field('a.title,a.type,a.id,a.top')
                ->group('a.id')
                ->order('a.top desc')
                ->select()
                ->toArray();

        if(!empty($data)){

            foreach ($data as &$v){

                $v['icon'] = $v['id'];

            }
        }

        return $data;

    }





}